package com.zretc.controller;

import com.zretc.entity.Courier;
import com.zretc.entity.HouseManager;
import com.zretc.service.CourierService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/courierLoginController")
public class CourierLoginController {
    private static final Logger logger = LoggerFactory.getLogger(CourierLoginController.class);
    @Autowired
    private CourierService courierService;

    @RequestMapping("/login")
    public @ResponseBody boolean login(@RequestParam(value = "courierName") String courierName,
                                       @RequestParam(value = "courierPsw") String courierPsw,
                                       HttpSession session){

        logger.info("====登录用户名：" + courierName);
        logger.info("====登录密码：" + courierPsw);

        Courier courier = courierService.login(courierName, courierPsw);

        if (courier == null){
            return false;
        }else{
            // 将用户信息存储session会话中
            session.setAttribute("courier",courier);
        }
        return true;
    }

    @RequestMapping("/logout")
    public @ResponseBody boolean logout(HttpSession session){

        // 清除session中指定属性
        session.removeAttribute("courier");
        return true;

    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ResponseBody
    public String register(String name,String psw){

        Courier courier = new Courier();
        courier.setCourierName(name);
        courier.setCourierPsw(psw);

        HouseManager houseManager = new HouseManager();
        houseManager.setHouseManagerId("123");

        courier.setHouseManager(houseManager);

        courierService.courierRegiseter(courier);

        return "true";
    }

    @RequestMapping("/deliveryView")
    @ResponseBody
    public Courier DeliveryView(HttpSession session){

        // 从session 中获取快递小哥的编号
        String courierId = (String) session.getAttribute("courierId");
        Courier courier = courierService.findDeliveryView("002");

        return courier;
    }

}
